//
//  InvestListViewModel.swift
//  cxd4iphone
//
//  Created by hexy on 12/1/15.
//  Copyright © 2015 hexy. All rights reserved.
//

import UIKit
import ReactiveCocoa

class InvestListViewModel: BaseTableListViewModel {

//    override init() {
//        
//        super.init()
//        
//        let vm1 = InvestCellViewModel(bidModel: BidModel(dict: ["":""]))
//        
//        self.sections = [[vm1, vm1, vm1, vm1,vm1,vm1,vm1,vm1,vm1,vm1,vm1,vm1,vm1,vm1]]
//    }
//    
    
    lazy var queryModel = QueryModel(dict: ["":""])
    
    lazy var data = [InvestCellViewModel]()
    static var page: Int = 1
    func loadBidList(pullDown: Bool) -> RACSignal {
        
        return RACSignal.createSignal({ (subscriber) -> RACDisposable! in
         
            self.queryModel.currentPage = pullDown ? 1 : InvestListViewModel.page++
            
            APIManager.loadBidList(self.queryModel).subscribeNext({ (result) -> Void in
                
//                    xx_JsonPrint(dict: result)
                    let array = result as! [[String: AnyObject]]
                
                    var arrM = [InvestCellViewModel]()
                    for dict in array {
                        
                        arrM.append(InvestCellViewModel(bidModel: BidModel(dict: dict)))
                    }
                
//                    xx_print(self.sections.count)
                
            
                    self.data = pullDown ? arrM : self.data + arrM
                    self.sections = [self.data]
                    subscriber.sendCompleted()
                
                }, error: { (error) -> Void in
                    
                    subscriber.sendError(error)
            })
            return RACDisposable(block: {})
        })
    }
    
    
    lazy var pickerViewModel: PickerViewModel = {
        
        let pvm = PickerViewModel()
        
        pvm.components = [PickerQueryViewModel.shared.loansDateQuery, PickerQueryViewModel.shared.loansRateQuery]
        return pvm
    }()
}
